﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.Data;
using DMS.Base;
using DMS.Business.Utility;
using System.Data.Common;

namespace DMS.Web.Common.Controls
{
    public partial class ActionLogList : System.Web.UI.UserControl
    {
        #region 成员属性
        /// <summary>
        /// 页大小限制
        /// </summary>
        public int PageLimte
        {
            get
            {
                return this.pager.PageSize;
            }
            set
            {
                this.pager.PageSize = value;
            }
        }
        #endregion

        #region 界面事件
        /// <summary>
        /// 控件加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            this.EnsureChildControls();

            if (!IsPostBack)
            {
                this.InitDataList();
            }
        }
        /// <summary>
        /// 翻页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void pager_PageChanged(object sender, EventArgs e)
        {
            this.InitDataList();
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            this.pager.GoToPage(1);
            this.InitDataList();
        }
        #endregion

        #region 内部方法
        protected override void EnsureChildControls()
        {
            if (this.ddlOperateCodeList.Items.Count == 0)
            {
                this.InitOperationCodeList();
            }
            base.EnsureChildControls();
        }
        public void InitOperationCodeList()
        {
            this.ddlOperateCodeList.Items.Clear();
            this.ddlOperateCodeList.DataSource = LogHelper.GetActionCode();
            this.ddlOperateCodeList.DataBind();

            ListItem item = new ListItem("全部类型", string.Empty);
            this.ddlOperateCodeList.Items.Insert(0, item);
        }
        public void InitDataList()
        {
            int total = 0;
            string condition = string.Empty;

            List<DbParameter> dbParams = new List<DbParameter>();
            if (!string.IsNullOrEmpty(this.ddlOperateCodeList.SelectedValue))
            {
                condition += " AND [OperationCode] = @OperationCode";
                DbParameter p_OperationCode = DBHelper.CreateParamer();
                p_OperationCode.ParameterName = "OperationCode";
                p_OperationCode.Value = this.ddlOperateCodeList.SelectedValue;
                dbParams.Add(p_OperationCode);
            }

            if (!string.IsNullOrEmpty(this.txtOperateUser.Text))
            {
                condition += " AND [Operator] LIKE @Operater";
                DbParameter p_Operater = DBHelper.CreateParamer();
                p_Operater.ParameterName = "Operater";
                p_Operater.Value = string.Format("%{0}%", (object) this.txtOperateUser.Text.Trim());
                dbParams.Add(p_Operater);
            }

            if (!string.IsNullOrEmpty(this.txtOperationContent.Text))
            {
                condition += " AND [OperationContent] LIKE @OperationContent";
                DbParameter p_OperaterContent = DBHelper.CreateParamer();
                p_OperaterContent.ParameterName = "OperationContent";
                p_OperaterContent.Value = string.Format("%{0}%", (object) this.txtOperationContent.Text.Trim());
                dbParams.Add(p_OperaterContent);
            }

            if (!string.IsNullOrEmpty(this.txtBeginTime.Text))
            {
                condition += " AND [OperateTime] >= @BeginTime";
                DbParameter p_BeginTime = DBHelper.CreateParamer();
                p_BeginTime.DbType = DbType.Date;
                p_BeginTime.ParameterName = "BeginTime";
                p_BeginTime.Value = Convert.ToDateTime((string) this.txtBeginTime.Text.Trim());
                dbParams.Add(p_BeginTime);
            }
            if (!string.IsNullOrEmpty(this.txtEndTime.Text))
            {
                condition += " AND [OperateTime] < @EndTime";
                DbParameter p_EndTime = DBHelper.CreateParamer();
                p_EndTime.DbType = DbType.Date;
                p_EndTime.ParameterName = "EndTime";
                p_EndTime.Value = Convert.ToDateTime((string) this.txtEndTime.Text.Trim()).AddDays(1);
                dbParams.Add(p_EndTime);
            }

            DataSet ds = LogHelper.GetActions(pager.PageSize, pager.CurrentPageIndex, out total, condition, dbParams);
            this.dgLogList.DataSource = ds;
            this.dgLogList.DataBind();

            this.pager.RecordCount = total;


            // 处理空数据
            if (total == 0)
            {
                this.dgLogList.Visible = false;
                this.pager.Visible = false;
                this.dvEmpty.Visible = true;
            }
            else
            {
                this.dgLogList.Visible = true;
                this.pager.Visible = true;
                this.dvEmpty.Visible = false;
            }
        }
        #endregion
    }
}